package com.cta.view.model.discount;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

import com.cta.constant.DiscountType;
import com.cta.entity.DiscountRate;
import com.cta.view.model.CallData;

public abstract class DiscountModel {

	protected DiscountRate orgDiscount;

	protected int discountValue;

	protected static final Logger log = LoggerFactory.getLogger(DiscountModel.class);

	public abstract void hasDiscount(CallData data) throws Exception;

	public DiscountModel(DiscountRate orgDiscount) {
		Assert.notNull(orgDiscount);
		this.orgDiscount = orgDiscount;
		if (StringUtils.isNotEmpty(this.orgDiscount.getValue())) {
			this.discountValue = Integer.valueOf(this.orgDiscount.getValue());
		}
	}

	public DiscountModel(DiscountRate orgDiscount, int discountVal) {
		Assert.notNull(orgDiscount);
		this.orgDiscount = orgDiscount;
		this.discountValue = discountVal;
	}

	public DiscountType getDiscountType() {
		return orgDiscount.getType();
	}

	@SuppressWarnings("unchecked")
	public static <T extends DiscountModel> T adapTo(DiscountRate rate, int discountVal) {
		if (rate.getType().equals(DiscountType.DATE)) {
			return (T) new DateDiscountModel(rate, discountVal);
		} else if (rate.getType().equals(DiscountType.DAY_OF_WEEK)) {
			return (T) new DayOfWeekDiscountModel(rate, discountVal);
		} else if (rate.getType().equals(DiscountType.HOURS)) {
			return (T) new HourDiscountModel(rate);
		} else {
			return null;
		}
	}

	public int getDiscountValue() {
		return discountValue;
	}
}
